<!--
Copyright (c) 2024 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>bufferSubData test to Wasm Memory 16GB in size.</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"> </script>
</head>
<body>
<canvas id="canvas" width="2" height="2" style="width: 40px; height: 40px;"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description(document.title);
debug("Tests that bufferSubData can be called on the end of a WebAssembly Memory of 16GB in size.");
debug("");
let wtu = WebGLTestUtils;
let gl = wtu.create3DContext("canvas", undefined, 2);

const PAGE = 65536;
const SIZE = 16 * 1024 * 1024 * 1024;
(() => {
  let view;
  try {
    view = new Uint8Array(new WebAssembly.Memory({ address: 'i64', initial: BigInt(SIZE / PAGE) }).buffer);
  } catch (e) {
    let fn = e instanceof RangeError ? testPassed : testFailed;
    fn(`Allocating ${SIZE} threw: ${e}`);
    return;
  }

  let expectedData = new Uint8Array([1, 2]);
  const length = expectedData.length;
  let srcOffset = SIZE - length;
  view.set(expectedData, srcOffset);
  const dstByteOffset = 4;

  let buf = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, buf);
  gl.bufferData(gl.ARRAY_BUFFER, 8, gl.STATIC_DRAW);
  try {
    gl.bufferSubData(gl.ARRAY_BUFFER, dstByteOffset, view, srcOffset, length);
  } catch (e) {
    testFailed(`bufferSubData from ${view.length} byte view threw: ${e}`);
    return;
  }
  wtu.glErrorShouldBe(gl, gl.NO_ERROR);

  let actualData = new Uint8Array(length);
  gl.getBufferSubData(gl.ARRAY_BUFFER, dstByteOffset, actualData);
  expectArray(actualData, expectedData);
})();

var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>
</body>
</html>
